package com.umpay.business.consume.impl;

import com.umpay.business.consume.ConsumeFinishOrderBusiness;
import com.umpay.dict.DataDict;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.merAccess.req.consume.ConsumeFinishOrderReq;
import com.umpay.dto.merAccess.res.consume.ConsumeFinishOrderRes;
import com.umpay.dto.merBusi.req.consume.ConsumeFinishOrderBusiReq;
import com.umpay.dto.merBusi.res.consume.ConsumeFinishOrderBusiRes;
import com.umpay.exception.BusinessException;
import com.umpay.feign.client.ConsumeBusiServerFeignClient;
import com.umpay.util.PropertyUtil;
import com.umpay.util.RpidUtils;
import com.umpay.util.StringUtils;
import com.umpay.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * @ClassName: ConsumeFinishOrderBusinessImpl  
 * @Description: 消费完成订单 
 * @author zhangtao  
 * @date Jun 7, 2018
 */
@Component
public class ConsumeFinishOrderBusinessImpl implements ConsumeFinishOrderBusiness {
    
	private static final Logger log = LoggerFactory.getLogger(ConsumeFinishOrderBusinessImpl.class);
	
	@Autowired
	private ConsumeBusiServerFeignClient consumeBusiServerFeignClient;
	
	@Autowired 
	private PropertyUtil propertyUtil;

	@Override
	public Message<ConsumeFinishOrderRes> finishOrder(ConsumeFinishOrderReq req) {
		log.info("【消费完成订单--步骤1】请求参数校验");
		checkRequestParameter(req);
		log.info("【消费完成订单--步骤2】请求业务线完成订单");
		Message<ConsumeFinishOrderBusiRes> merBusiRes = requestMerBusiFinishOrder(req);
		log.info("【消费完成订单--步骤3】响应结果通知商户");
		return assembleResponseData(merBusiRes);
	}
	private Message<ConsumeFinishOrderBusiRes> requestMerBusiFinishOrder(ConsumeFinishOrderReq req){
		ConsumeFinishOrderBusiReq merBusiReq = new ConsumeFinishOrderBusiReq();
		merBusiReq.setRpid(RpidUtils.getRpid());
		merBusiReq.setMerId(StringUtils.trim(req.getMer_id()));
		merBusiReq.setBproductId(StringUtils.trim(propertyUtil.getProperty("bsp.bproductid"))); //基础业务产品
		merBusiReq.setReqDate(TimeUtil.date8());
		merBusiReq.setReqTime(TimeUtil.time6());
		merBusiReq.setTradeNo(StringUtils.trim(req.getTrade_no()));
		merBusiReq.setSplitOrderId(StringUtils.trim(req.getSplit_order_id()));
		merBusiReq.setSplitOrderDate(StringUtils.trim(req.getSplit_mer_date()));
		merBusiReq.setVersion(DataDict.VERSION);
		log.info(String.format("消费完成订单：调用远程服务接口请求参数为:%s", merBusiReq));
		Message<ConsumeFinishOrderBusiRes> merBusiRes = consumeBusiServerFeignClient.finishOrder(merBusiReq);
		log.info(String.format("消费完成订单：调用远程服务接口请求参数为:%s", merBusiReq));
		return merBusiRes;
	}
	private Message<ConsumeFinishOrderRes> assembleResponseData(Message<ConsumeFinishOrderBusiRes> merBusiRes){
		Message<ConsumeFinishOrderRes> resMessage = new Message<ConsumeFinishOrderRes>();
		Meta resMeta = new Meta();
		if(merBusiRes != null) {
			Meta merBusiRet = merBusiRes.getMeta();
			String retCode = merBusiRet.getRet_code();
			String retMsg = merBusiRet.getRet_msg();
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			ConsumeFinishOrderBusiRes resInfo = merBusiRes.getData();
			ConsumeFinishOrderRes merAccessRes = new ConsumeFinishOrderRes();
			if(resInfo != null) {
				merAccessRes.setMer_id(resInfo.getMerId());//商户号
				merAccessRes.setTrade_no(resInfo.getTradeNo());
				merAccessRes.setVersion(resInfo.getVersion());
				merAccessRes.setSplit_result(resInfo.getSplitResult());
			}
			resMessage.setData(merAccessRes);
		}else {
			resMeta.setRet_code(MerAccessEC.RESULT_UNKNOWN);
			resMeta.setRet_msg("交易结果不明");
		}
		resMessage.setMeta(resMeta);
		return resMessage;
	}
	private void checkRequestParameter(ConsumeFinishOrderReq req) {
		String spliOrderId  = StringUtils.trim(req.getSplit_order_id());
		String spliOrderDate = StringUtils.trim(req.getSplit_mer_date());
		//校验spliOrderId和spliOrderDate必须同时为空，或者不为空
		if((StringUtils.isNotEmpty(spliOrderId) && StringUtils.isEmpty(spliOrderDate))
		     ||(StringUtils.isEmpty(spliOrderId) && StringUtils.isNotEmpty(spliOrderDate))){
			throw new BusinessException(MerAccessEC.REQPARAM_ERR, "split_order_id和split_mer_date必须同时为空，或者不为空");
		}
	}
	
}
